#!/usr/bin/env python
# -*- coding: utf-8 -*-
# ==================================================
# @File    : summary
# @IDE     : PyCharm
# @Author  : liuxiangui
# @Date    : 2024/2/3 16:01
# @Describe: 
# ==================================================
import datetime
import json

class AllureReportSummary:
    def __init__(self, file_path: str):
        with open(file_path, 'r', encoding='utf-8') as f:
            content = f.read()
        self._summaryData = json.loads(content)

    @property
    def case_total_count(self):
        return self._summaryData["statistic"]["total"]

    @property
    def case_pass_count(self):
        return self._summaryData["statistic"]["passed"]

    @property
    def case_failed_count(self):
        return self._summaryData["statistic"]["failed"]

    @property
    def case_pass_rate(self):
        return round(self.case_pass_count / self.case_total_count * 100, 2)

    @property
    def start_time(self):
        times = self._summaryData["time"]["start"]
        return datetime.datetime.fromtimestamp(times / 1000).strftime("%Y-%m-%d %H:%M:%S")

    @property
    def end_time(self):
        times = self._summaryData["time"]["stop"]
        return datetime.datetime.fromtimestamp(times / 1000).strftime("%Y-%m-%d %H:%M:%S")

    @property
    def excute_time(self):
        ms = self._summaryData["time"]["sumDuration"]
        hour = ms // (60 * 60 * 1000)
        minute = (ms - hour * 60 * 60 * 1000) // (60 * 1000)
        second = (ms - minute * 60 * 1000) // 1000
        t = ""
        if hour != 0:
            t += f"{hour}小时"
        if minute != 0:
            t += f"{minute}分"
        if second != 0:
            t += f"{second}秒"
        return t or f"{ms}毫秒"
